using UnityEngine;
using UnityEditor;

namespace GameToolkit.Editor
{
    [CustomPropertyDrawer(typeof(RangeFieldAttribute))]
    public class RangeFieldDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);
            int lv = EditorGUI.indentLevel;
            var att = attribute as RangeFieldAttribute;
            position = EditorGUI.PrefixLabel(position, label);
            EditorGUI.indentLevel = 0;

            if (property.propertyType == SerializedPropertyType.Vector2)
            {
                if (att.SliderMode)
                {
                    float min = property.vector2Value.x;
                    float max = property.vector2Value.y;
                    var ts = Mathf.Min(60f, position.width * 0.3f - 2);
                    var w = position.width - 4 - ts * 2;
                    min = EditorGUI.FloatField(new Rect(position.x, position.y, ts, position.height), min);
                    max = EditorGUI.FloatField(new Rect(position.xMax - ts, position.y, ts, position.height), max);
                    EditorGUI.MinMaxSlider(new Rect(position.x + 2 + ts, position.y, w, position.height), ref min, ref max, att.Min, att.Max);
                    property.vector2Value = new Vector2(min, max);
                }
                else
                {
                    var ts = 27f;
                    float w = Mathf.Min(100, (position.width - ts * 2) * 0.5f - 10);
                    float sp = position.width - w * 2 - ts * 2;
                    EditorGUI.LabelField(new Rect(position.center.x - sp * 0.5f + 2, position.y, sp - 4, position.height), DevilEditorUtility.EmptyContent, (GUIStyle)"horizontalslider");
                    var v = property.vector2Value;
                    v.y = Mathf.Min(v.y, att.Max);
                    v.x = Mathf.Max(v.x, att.Min);

                    EditorGUI.LabelField(new Rect(position.x, position.y, ts, position.height), "Min");
                    EditorGUI.LabelField(new Rect(position.xMax - ts, position.y, ts, position.height), "Max");
                    v.x = Mathf.Clamp(EditorGUI.FloatField(new Rect(position.x + ts, position.y, w, position.height), v.x), att.Min, v.y);
                    v.y = Mathf.Clamp(EditorGUI.FloatField(new Rect(position.xMax - w - ts, position.y, w, position.height), v.y), v.x, att.Max);
                    property.vector2Value = v;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Vector2Int)
            {
                if (att.SliderMode)
                {
                    int min = property.vector2IntValue.x;
                    int max = property.vector2IntValue.y;
                    var ts = Mathf.Min(60f, position.width * 0.3f - 2);
                    var w = position.width - 4 - ts * 2;
                    min = EditorGUI.IntField(new Rect(position.x, position.y, ts, position.height), min);
                    max = EditorGUI.IntField(new Rect(position.xMax - ts, position.y, ts, position.height), max);
                    float m1 = min, m2 = max;
                    EditorGUI.MinMaxSlider(new Rect(position.x + 2 + ts, position.y, w, position.height), ref m1, ref m2, att.Min, att.Max);
                    min = Mathf.RoundToInt(m1);
                    max = Mathf.RoundToInt(m2);
                    property.vector2IntValue = new Vector2Int(min, max);
                }
                else
                {
                    var ts = 27f;
                    float w = Mathf.Min(100, (position.width - ts * 2) * 0.5f - 10);
                    float sp = position.width - w * 2 - ts * 2;
                    EditorGUI.LabelField(new Rect(position.center.x - sp * 0.5f + 2, position.y, sp - 4, position.height), DevilEditorUtility.EmptyContent, (GUIStyle)"horizontalslider");
                    var v = property.vector2IntValue;
                    v.y = Mathf.Min(v.y, (int)att.Max);
                    v.x = Mathf.Max(v.x, (int)att.Min);

                    EditorGUI.LabelField(new Rect(position.x, position.y, ts, position.height), "Min");
                    EditorGUI.LabelField(new Rect(position.xMax - ts, position.y, ts, position.height), "Max");
                    v.x = Mathf.Clamp(EditorGUI.IntField(new Rect(position.x + ts, position.y, w, position.height), v.x), (int)att.Min, v.y);
                    v.y = Mathf.Clamp(EditorGUI.IntField(new Rect(position.xMax - w - ts, position.y, w, position.height), v.y), v.x, (int)att.Max);
                    property.vector2IntValue = v;
                }
            }
            else
            {
                EditorGUI.LabelField(position, label.text, "Use Vector2 Instead of " + property.propertyType);
            }
            EditorGUI.indentLevel = lv;
            EditorGUI.EndProperty();
        }

    }
}